package com.woniuxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.woniuxy.annotations.RequirePerm;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.OrderService;
import com.woniuxy.service.OrderServiceByProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 邓宇昂
 * @Do: TODO
 * @DateTime: 22/05/30/0030 下午 12:05
 * @Component: 成都蜗牛学苑
 **/
@RestController
@RequestMapping("/order")
@RefreshScope //只要有这个注解的类，里面的配置信息，就支持热刷新
public class OrderController {
    @Value("${woniu.config}")
    private String myConfig;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderServiceByProduct orderServiceByProduct;
    @PostMapping("/{pid}/{num}")
    @RequirePerm(perms = {"order::add","order::edit"})
    public ResponseEntity generateOrder(@PathVariable Integer pid,@PathVariable Integer num){
        return orderService.generateOrder(pid,num);
    }
    @GetMapping("/{name}/{minPrice}/{maxPrice}")
    public ResponseEntity findProductList(@PathVariable String name,@PathVariable double minPrice,@PathVariable double maxPrice){
        return orderServiceByProduct.findProductList(name,minPrice,maxPrice);
    }
    @GetMapping("/showConfig")
    @SentinelResource(value = "yuema",fallback = "fallback",blockHandler = "currentLimiting")
    public ResponseEntity showConfig() throws FlowException {
        throw new FlowException("抛出异常");
    }
    public ResponseEntity fallback(){
        return new ResponseEntity("马宇航写的bug");
    }
    public ResponseEntity currentLimiting(FlowException e){
        return new ResponseEntity("轻一点，慢一点！"+e);
    }

}